<?php

namespace App\Http\Controllers\Backend;

use App\Http\Repositories\ProjectRepository;
use App\Http\Repositories\ProjectTypeRepository;
use App\Http\Controllers\Controller;
use App\Http\Requests\Backend\ProjectRequest;
use Illuminate\Http\Request;

class ProjectController extends Controller
{
    protected $projectTypeRepository;
    protected $projectRepository;

    public function __construct(ProjectTypeRepository $projectTypeRepository, ProjectRepository $projectRepository)
    {
        $this->projectTypeRepository = $projectTypeRepository;
        $this->projectRepository = $projectRepository;
    }

    /**
     * 获取项目列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request)
    {
        $project_type = $this->projectTypeRepository->getType(true);
        $list = $this->projectRepository->getList($request->all());
        return responseResult(compact('project_type', 'list'), self::SUCCESS_CODE);
    }

    /**
     * 添加项目
     * @param ProjectRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function create(ProjectRequest $request)
    {
        $result = $this->projectRepository->edit($request->all());
        return responseResult([], $result ? self::SUCCESS_CODE : self::ERROR_CODE);
    }

    /**
     * 更新项目
     * @param ProjectRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function update(ProjectRequest $request)
    {
        $result = $this->projectRepository->edit($request->except("id"), $request->get("id"));
        return responseResult([], $result ? self::SUCCESS_CODE : self::ERROR_CODE);
    }

    /**
     * 删除项目
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function del($id)
    {
        $result = $this->projectRepository->del($id);
        return responseResult([], $result ? self::SUCCESS_CODE : self::ERROR_CODE);
    }

}
